import { getEmitter } from "./emitter";
import {
    SlateRange,
    SlateEditor,
    SlateTransforms,
    SlatePath,
    DomEditor,
} from "@wangeditor/editor";
import {throttle} from "lodash";

export function selectionTrimEnd(editor) {
    const { selection } = editor;

    if (selection) {
        const [start, end] = SlateRange.edges(selection);
        const wordRange = SlateEditor.range(editor, start, end);
        const wordText = SlateEditor.string(editor, wordRange);

        const trimmedText = wordText.trimEnd();

        if (trimmedText !== wordText) {
            const offset = wordText.length - trimmedText.length;
            const newEnd = { ...end, offset: end.offset - offset };
            const newSelection = { ...selection, anchor: start, focus: newEnd };

            SlateTransforms.select(editor, newSelection);
        }
    }
}

export function insertNodeSpace(editor, range) {
    SlateEditor.withoutNormalizing(editor, () => {
        const startPoint = SlateEditor.start(editor, range);
        const endPoint = SlateEditor.end(editor, range);
        SlateTransforms.insertText(editor, " ", { at: startPoint });
        SlateTransforms.insertText(editor, " ", {
            at: { path: endPoint.path, offset: endPoint.offset + 1 },
        });
        SlateTransforms.select(editor, {
            anchor: { path: startPoint.path, offset: startPoint.offset + 1 },
            focus: { path: endPoint.path, offset: endPoint.offset + 1 },
        });
    });
}

export function removeNodeSpace(editor, path) {
    SlateEditor.withoutNormalizing(editor, () => {
        const startPoint = SlateEditor.before(editor, path);
        const endPoint = SlateEditor.after(editor, path);
        if (!startPoint || !endPoint) return;
        const startRange = {
            anchor: { path: startPoint.path, offset: startPoint.offset - 1 },
            focus: { path: startPoint.path, offset: startPoint.offset },
        };
        const endRange = {
            anchor: { path: endPoint.path, offset: endPoint.offset },
            focus: { path: endPoint.path, offset: endPoint.offset + 1 },
        };
        if (SlateEditor.string(editor, startRange) === " ") {
            SlateTransforms.delete(editor, { at: startRange });
        }
        if (SlateEditor.string(editor, endRange) === " ") {
            SlateTransforms.delete(editor, { at: endRange });
        }
    });
}



export function handleDeleteNode(editor, elem) {
    console.log("handleDeleteNode")
    const path = DomEditor.findPath(editor, elem);
    SlateTransforms.delete(editor, { at: path });
}

export function handleUnwrapNodes(editor, elem) {
    console.log("handleUnwrapNodes")
    const path = DomEditor.findPath(editor, elem);
    removeNodeSpace(editor, path);
    SlateTransforms.unwrapNodes(editor, { at: path });  
}


export function handleSSMLRemarkClick(editor, elem) {
    // console.log("handleSSMLRemarkClick")
    if (!editor.isFocused()) {
        editor.focus();
    }
    // console.log("handleSSMLRemarkClick")
    editor.select(DomEditor.findPath(editor, elem));
    getEmitter(editor).emit("ssml-remark-click", editor, elem); 
}





export function handleReplaceMarkNode(editor, elem, remark) {
    if (remark){
        const newNode = {
            type: "ssml-popover",
            alphabet: "py",
            ph: elem.ph,
            remark: remark,
            children: elem.children,
        };
        handleReplaceNode(editor, elem, newNode)
    }else{
        const newNode = {
            type: "ssml-mark",
            alphabet: "py",
            ph: elem.ph,
            remark: remark,
            children: elem.children,
        };
        handleReplaceNode(editor, elem, newNode)
    }
    

    
}

export function handleReplacePinYinNode(editor, elem, opt) {
    const newNode = {
        type: "ssml-phoneme",
        alphabet: "py",
        ph: opt.value,
        remark: opt.label,
        children: elem.children,
    };
    handleReplaceNode(editor, elem, newNode)

}
// 移除当前选中，然后插入新的节点
export function handleReplaceNode(editor, elem, newNode){
    const path = DomEditor.findPath(editor, elem);
    editor.select(path);
    removeNodeSpace(editor, path);
    SlateTransforms.unwrapNodes(editor, { at: path }) 
    editor.insertNode(newNode);
}


// export function handleDeleteNode(editor, elem) {
//     return throttle((event) => {
//         event.preventDefault();
//         const path = DomEditor.findPath(editor, elem);
//         SlateTransforms.delete(editor, { at: path });
//     });
// }

// export function handleUnwrapNodes(editor, elem) {
//     return throttle((event) => {
//         event.preventDefault();
//         const path = DomEditor.findPath(editor, elem);
//         removeNodeSpace(editor, path);
//         SlateTransforms.unwrapNodes(editor, { at: path });
//     });
// }


// export function handleSSMLRemarkClick(editor, elem) {
//     return throttle((event) => {
//         event.preventDefault();
//         if (!editor.isFocused()) editor.focus();
//         editor.select(DomEditor.findPath(editor, elem));
//         getEmitter(editor).emit("ssml-remark-click", editor, elem);
//     });
// }
